<?php
namespace Admin\Model;
use Think\Model;
class DataModel extends Model{
    //添加数据
    public function addData($post,$file){
        //先判断是否有文件需要处理
        if(!$file['error']){
            //取出类别文件夹名
            $model = M('Category');
            $category = $model -> find($post['category_id']);
            //dump($rootPath);die;
            //定义配置
            $cfg = array(
                //'maxSize' => 3145728,
                'rootPath' => './Uploads/',
                //'savePath' => '',
                'saveName' => array('uniqid',''),
                'exts' => 'jpg',
                //'mimes' => 'image/jpeg', //没用
                'autoSub' => true,
                'subName' => $category['folder'],
                //'rootPath'  =>  WORKING_PATH . UPLOAD_ROOT_PATH,
            );
            //dump($cfg);die;
            //实例化上传类
            $upload = new \Think\Upload($cfg);
            //开始上传
            $info = $upload -> uploadOne($file);
            //dump($info);die;
            //判断是否上传成功
            if($info){
                //补全kid(文件名)
                $post['kid'] = $info['savename'];  //保存的文件名称
            }
        }
        //dump($file);die;
        //补全addtime字段
        $post['addtime'] = time();
        //dump($post);die;
        return $this -> add($post);
    }
}